<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 39. Boost.STLInterfaces</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="boost_staticassert/test.html" title="Test Programs">
<link rel="next" href="boost_stlinterfaces/this_library_s_relationship_to_boost_iterator.html" title="This Library's Relationship to Boost.Iterator">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_staticassert/test.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_stlinterfaces/this_library_s_relationship_to_boost_iterator.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="stl_interfaces"></a>Chapter 39. Boost.STLInterfaces</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Zach</span> <span class="surname">Laine</span>
</h3></div></div>
<div><p class="copyright">Copyright © 2019 T. Zachary Laine</p></div>
<div><div class="legalnotice">
<a name="stl_interfaces.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="stl_interfaces.html#boost_stlinterfaces.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/this_library_s_relationship_to_boost_iterator.html">This
    Library's Relationship to Boost.Iterator</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/the__v1__and__v2__namespaces__and_the_use_of_c__20_concepts.html">The
    <code class="computeroutput"><span class="identifier">v1</span></code> and <code class="computeroutput"><span class="identifier">v2</span></code>
    Namespaces, and the Use of C++20 Concepts</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/tutorial___iterator_interface_.html">Tutorial:
    <code class="computeroutput"><span class="identifier">iterator_interface</span></code></a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/tutorial___view_interface_.html">Tutorial:
    <code class="computeroutput"><span class="identifier">view_interface</span></code></a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/tutorial___sequence_container_interface_.html">Tutorial:
    <code class="computeroutput"><span class="identifier">sequence_container_interface</span></code></a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/tutorial___reverse_iterator_.html">Tutorial:
    <code class="computeroutput"><span class="identifier">reverse_iterator</span></code></a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html">Examples</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html#boost_stlinterfaces.examples.random_access_iterator">Random
      Access Iterator</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html#boost_stlinterfaces.examples.mutable_and_constant_iterator_interoperability">Mutable
      and Constant Iterator Interoperability</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html#boost_stlinterfaces.examples.zip_iterator___proxy_iterator">Zip
      Iterator / Proxy Iterator</a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html#boost_stlinterfaces.examples.reimplementing__back_insert_iterator_">Reimplementing
      <code class="computeroutput"><span class="identifier">back_insert_iterator</span></code></a></span></dt>
<dt><span class="section"><a href="boost_stlinterfaces/examples.html#boost_stlinterfaces.examples.reimplementing__reverse_iterator_">Reimplementing
      <code class="computeroutput"><span class="identifier">reverse_iterator</span></code></a></span></dt>
</dl></dd>
<dt><span class="section"><a href="stl_interfaces/compiler_support.html">Compiler Support</a></span></dt>
<dt><span class="section"><a href="stl_interfaces/reference.html">Reference</a></span></dt>
<dd><dl><dt><span class="section"><a href="stl_interfaces/reference.html#headers">Headers</a></span></dt></dl></dd>
<dt><span class="section"><a href="boost_stlinterfaces/rationale.html">Rationale</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_stlinterfaces.introduction"></a><a class="link" href="stl_interfaces.html#boost_stlinterfaces.introduction" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      Writing STL iterators, views, and containers is surprisingly hard. There are
      a lot of things that can subtly go wrong. It is also very tedious, which of
      course makes it error-prone.
    </p>
<p>
      Iterators have numerous typedefs and operations, even though all the operations
      of a given iterator can be implemented in terms of at most four operations
      (and usually only three). Writing all the other operations yields very similar-looking
      code that is hard to review, and all but requires that you write full-coverage
      tests for each iterator.
    </p>
<p>
      Writing view types like those found in <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ranges</span></code>
      is also laborious, considering that most of each view type's API can be derived
      from <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code>
      and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>.
      C++20 has a template that does exactly this, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ranges</span><span class="special">::</span><span class="identifier">view_interface</span></code>;
      Boost.STLInterfaces provides a pre-C++20-friendly implementation.
    </p>
<p>
      Most daunting of all is the task of writing a type or template that meets the
      container requirements in the standard. Boost.STLInterfaces provides another
      template called <code class="computeroutput">sequence_container_interface</code>
      that reduces the implementation and testing burden dramatically.
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        C++20 versions of <code class="computeroutput">iterator_interface</code> and <code class="computeroutput">sequence_container_interface</code>
        are provided (C++20 provides <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">view_interface</span></code>).
        These are constrained templates using C++20 concepts. These are in the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">v2</span></code> namespace, and are considered experimental,
        because at the time of this writing, no C++20-conforming compiler exists.
      </p></td></tr>
</table></div>
<h4>
<a name="boost_stlinterfaces.introduction.h0"></a>
      <span class="phrase"><a name="boost_stlinterfaces.introduction.a_quick_example"></a></span><a class="link" href="stl_interfaces.html#boost_stlinterfaces.introduction.a_quick_example">A
      Quick Example</a>
    </h4>
<p>
      Here is an example of the iterator portion of the library. Let's say that we
      wanted to make a random access iterator that represents a string of arbitrary
      length constructed by repeating a shorter string. Let's call this iterator
      <code class="computeroutput"><span class="identifier">repeated_chars_iterator</span></code>. Here
      it is in action:
    </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">repeated_chars_iterator</span> <span class="identifier">first</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">0</span><span class="special">);</span> <span class="comment">// 3 is the length of "foo", 0 is this iterator's position.</span>
<span class="identifier">repeated_chars_iterator</span> <span class="identifier">last</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">7</span><span class="special">);</span>  <span class="comment">// Same as above, but now the iterator's position is 7.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">result</span><span class="special">));</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">result</span> <span class="special">==</span> <span class="string">"foofoof"</span><span class="special">);</span>
</pre>
<p>
    </p>
<p>
      There's nothing in the standard library that gets us that kind of behavior,
      so we have to write it. This library seeks to turn what we write from this:
    </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">repeated_chars_iterator</span>
<span class="special">{</span>
    <span class="keyword">using</span> <span class="identifier">value_type</span> <span class="special">=</span> <span class="keyword">char</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">difference_type</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">pointer</span> <span class="special">=</span> <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*;</span>
    <span class="keyword">using</span> <span class="identifier">reference</span> <span class="special">=</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">iterator_category</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">random_access_iterator_tag</span><span class="special">;</span>

    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">:</span>
        <span class="identifier">first_</span><span class="special">(</span><span class="keyword">nullptr</span><span class="special">),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="number">0</span><span class="special">),</span>
        <span class="identifier">n_</span><span class="special">(</span><span class="number">0</span><span class="special">)</span>
    <span class="special">{}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span><span class="special">(</span>
        <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">first</span><span class="special">,</span>
        <span class="identifier">difference_type</span> <span class="identifier">size</span><span class="special">,</span>
        <span class="identifier">difference_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">:</span>
        <span class="identifier">first_</span><span class="special">(</span><span class="identifier">first</span><span class="special">),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="identifier">size</span><span class="special">),</span>
        <span class="identifier">n_</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span>
    <span class="special">{}</span>

    <span class="keyword">constexpr</span> <span class="identifier">reference</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">first_</span><span class="special">[</span><span class="identifier">n_</span> <span class="special">%</span> <span class="identifier">size_</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="keyword">constexpr</span> <span class="identifier">value_type</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">difference_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">first_</span><span class="special">[(</span><span class="identifier">n_</span> <span class="special">+</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">%</span> <span class="identifier">size_</span><span class="special">];</span>
    <span class="special">}</span>

    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">++()</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="special">++</span><span class="identifier">n_</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="keyword">operator</span><span class="special">++(</span><span class="keyword">int</span><span class="special">)</span><span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">retval</span> <span class="special">=</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
        <span class="special">++*</span><span class="keyword">this</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">retval</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="identifier">difference_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="identifier">n_</span> <span class="special">+=</span> <span class="identifier">n</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">--()</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="special">--</span><span class="identifier">n_</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="keyword">operator</span><span class="special">--(</span><span class="keyword">int</span><span class="special">)</span><span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">retval</span> <span class="special">=</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
        <span class="special">--*</span><span class="keyword">this</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">retval</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">-=(</span><span class="identifier">difference_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="identifier">n_</span> <span class="special">-=</span> <span class="identifier">n</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span><span class="special">.</span><span class="identifier">first_</span> <span class="special">==</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">first_</span> <span class="special">&amp;&amp;</span> <span class="identifier">lhs</span><span class="special">.</span><span class="identifier">n_</span> <span class="special">==</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">n_</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="special">!(</span><span class="identifier">lhs</span> <span class="special">==</span> <span class="identifier">rhs</span><span class="special">);</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span><span class="special">.</span><span class="identifier">first_</span> <span class="special">==</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">first_</span> <span class="special">&amp;&amp;</span> <span class="identifier">lhs</span><span class="special">.</span><span class="identifier">n_</span> <span class="special">&lt;</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">n_</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;=(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span> <span class="special">==</span> <span class="identifier">rhs</span> <span class="special">||</span> <span class="identifier">lhs</span> <span class="special">&lt;</span> <span class="identifier">rhs</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">rhs</span> <span class="special">&lt;</span> <span class="identifier">lhs</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;=(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">rhs</span> <span class="special">&lt;=</span> <span class="identifier">lhs</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span>
    <span class="keyword">operator</span><span class="special">+(</span><span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">difference_type</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span> <span class="special">+=</span> <span class="identifier">rhs</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span>
    <span class="keyword">operator</span><span class="special">+(</span><span class="identifier">difference_type</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">rhs</span> <span class="special">+=</span> <span class="identifier">lhs</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span>
    <span class="keyword">operator</span><span class="special">-(</span><span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">difference_type</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span> <span class="special">-=</span> <span class="identifier">rhs</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">friend</span> <span class="keyword">constexpr</span> <span class="identifier">difference_type</span> <span class="keyword">operator</span><span class="special">-(</span>
        <span class="identifier">repeated_chars_iterator</span> <span class="identifier">lhs</span><span class="special">,</span> <span class="identifier">repeated_chars_iterator</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">lhs</span><span class="special">.</span><span class="identifier">n_</span> <span class="special">-</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">n_</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">first_</span><span class="special">;</span>
    <span class="identifier">difference_type</span> <span class="identifier">size_</span><span class="special">;</span>
    <span class="identifier">difference_type</span> <span class="identifier">n_</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
      (that's a lot of code!) into this:
    </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">stl_interfaces</span><span class="special">::</span><span class="identifier">iterator_interface</span><span class="special">&lt;</span>
                                     <span class="identifier">repeated_chars_iterator</span><span class="special">,</span>
                                     <span class="identifier">std</span><span class="special">::</span><span class="identifier">random_access_iterator_tag</span><span class="special">,</span>
                                     <span class="keyword">char</span><span class="special">,</span>
                                     <span class="keyword">char</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span><span class="special">()</span> <span class="keyword">noexcept</span> <span class="special">:</span>
        <span class="identifier">first_</span><span class="special">(</span><span class="keyword">nullptr</span><span class="special">),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="number">0</span><span class="special">),</span>
        <span class="identifier">n_</span><span class="special">(</span><span class="number">0</span><span class="special">)</span>
    <span class="special">{}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span><span class="special">(</span>
        <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">difference_type</span> <span class="identifier">size</span><span class="special">,</span> <span class="identifier">difference_type</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">noexcept</span> <span class="special">:</span>
        <span class="identifier">first_</span><span class="special">(</span><span class="identifier">first</span><span class="special">),</span>
        <span class="identifier">size_</span><span class="special">(</span><span class="identifier">size</span><span class="special">),</span>
        <span class="identifier">n_</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span>
    <span class="special">{}</span>

    <span class="keyword">constexpr</span> <span class="keyword">char</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">first_</span><span class="special">[</span><span class="identifier">n_</span> <span class="special">%</span> <span class="identifier">size_</span><span class="special">];</span> <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="identifier">repeated_chars_iterator</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span> <span class="identifier">i</span><span class="special">)</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="identifier">n_</span> <span class="special">+=</span> <span class="identifier">i</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="keyword">this</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="keyword">operator</span><span class="special">-(</span><span class="identifier">repeated_chars_iterator</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">n_</span> <span class="special">-</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">n_</span><span class="special">;</span>
    <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
    <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">first_</span><span class="special">;</span>
    <span class="identifier">difference_type</span> <span class="identifier">size_</span><span class="special">;</span>
    <span class="identifier">difference_type</span> <span class="identifier">n_</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
    </p>
<p>
      Ah, that's better. Both of these definitions for <code class="computeroutput"><span class="identifier">repeated_chars_iterator</span></code>
      have the same semantics and performance profile. It's just a lot less code
      to write the second one, and writing the second one is more novice-friendly.
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        Boost.STLInterfaces's <code class="computeroutput"><span class="identifier">iterator_interface</span></code>
        implements iterators that model the C++20 iterator concepts.
      </p></td></tr>
</table></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: December 02, 2021 at 06:54:13 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_staticassert/test.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_stlinterfaces/this_library_s_relationship_to_boost_iterator.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
